import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


/**
 * 功能：生成maven批量安装jar的语句
 */
public class MyTest {

    //批量安装jar所在目录
    //private static String filePath = "/Users/jiangtao/Downloads/rdp-jars";
    private static String filePath = "/Users/jiangtao/183-merg-jars";


    public static void main(String[] args) throws IOException {
        File f = new File(filePath);
        File[] fils = f.listFiles();

        printInstallFile(fils);
        //printDeployFile(fils);

        //String fileName = "rdp.b.util-0.7.2.0.0-20180111.021047-55.jar";
        //String artifactId = fileName.split("-")[0];
        //String version = fileName.substring(artifactId.length() + 1, fileName.length() - ".jar".length());
        //
        //System.out.println(artifactId);
        //System.out.println(version);

    }

    private static void printDeployFile(File[] fils) throws IOException {
        printFile(fils, "deploy");
    }

    private static void printInstallFile(File[] fils) throws IOException {
        printFile(fils, "install");
    }

    /*
     * <rdp.ec.version>0.0.3.2.0-SNAPSHOT</rdp.ec.version>
     * <rdp.version>0.7.2.0.0-SNAPSHOT</rdp.version>
     *
     *
     *
     * <dependency>
     *      <groupId>com.rongzer.rdp</groupId>
     *      <artifactId>rdp.c.notification</artifactId>
     *      <version>${rdp.version}</version>
     *      <scope>compile</scope>
     * </dependency>
     *
     * <dependency>
     *      <groupId>com.rongzer.rdp.ec</groupId>
     *      <artifactId>ec.b.common</artifactId>
     *      <version>${rdp.ec.version}</version>
     *      <scope>compile</scope>
     * </dependency>
     *
     *      mvn install:install-file -DgroupId=com.rongzer.rdp -DartifactId=itext-asian -Dversion=5.2.0 -Dpackaging=jar -Dfile=itext-asian-5.2.0.jar
     *      参数：--settings D:\Java\Repository\settings.xml
     *
     *       mvn install:install-file --settings /Users/jiangtao/.m2/settings.xml -DgroupId=com.rongzer.rdp -DartifactId=itext-asian -Dversion=5.2.0 -Dpackaging=jar -Dfile=itext-asian-5.2.0.jar
     *       mvn deploy:deploy-file -Dmaven.test.skip=true -DgroupId=cn.conac.rc.api.client -DartifactId=rc-gateway-api-client -Dversion=1.0.0 -Dpackaging=jar -Dfile=D:\temp\rc-gateway-api-client.jar -Durl=http://你的地址/nexus/content/repositories/releases/ -DrepositoryId=releases
     *
     *      /Users/jiangtao/Downloads/rdp-jars/rdp.c.rule-0.7.2.0.0-SNAPSHOT.jar
     */
    public static void printFile(File[] files, String opt) throws IOException {
        //
        //Stream<String> files = linkedList.stream().flatMap(a -> Stream.of(a.getPath()));
        //
        //files.flatMap(a -> Stream.of(a.startsWith("rdp"))).forEach(System.out::println);
        //

        //  /Users/jiangtao/Downloads/rdp-jars/rdp.c.rule-0.7.2.0.0-SNAPSHOT.jar
        //  org/springframework/spring-jms/4.2.8.RELEASE/spring-jms-4.2.8.RELEASE.jar

        String install_template = "mvn install:install-file --settings /Users/jiangtao/.m2/settings.xml -Dfile=jarfile -DgroupId=gid -DartifactId=aid -Dversion=vsn -Dpackaging=jar";
        String deploy_template = "mvn deploy:deploy-file -Dmaven.test.skip=true -DgroupId=gid -DartifactId=aid -Dversion=vsn -Dpackaging=jar -Dfile=jarfile -Durl=http://nexus.seehoo.cn/nexus/repository/maven-releases/ -DrepositoryId=releases";

        String opt_tmp;

        if ("install".equals(opt))
            opt_tmp = install_template;
        else
            opt_tmp = deploy_template;


        //Stream<String> lines = Files.lines( FileSystems.getDefault().getPath("src", "MyTest.java"), StandardCharsets.UTF_8);
        ////lines.forEach(System.out::println);
        //Stream<Object> words = lines.flatMap(line -> Stream.of(line.split("\\{")));
        //words.forEach( System.out::println);

        System.out.println(Arrays.asList(files));

        List<String> processList = new ArrayList<String>();
        for (int i = 0; i < files.length; i++) {


            if (files[i].isFile()) {
                String fileName = files[i].getName();

                //rdp.c.rule-0.7.2.0.0-SNAPSHOT.jar
                //rdp.c.rule-0.7.2.0.0-SNAPSHOT.jar
                //ec.c.cms-0.0.3.2.0-SNAPSHOT.jar
                //rdp.b.util-0.7.2.0.0-20180111.021047-55.jar
                String artifactId = fileName.split("-")[0];
                String version = fileName.substring(artifactId.length() + 1, fileName.length() - ".jar".length());

                //System.out.println(Arrays.asList(split));

                String rst = opt_tmp.replace("jarfile", files[i].getPath())
                        .replace("gid", fileName.startsWith("ec.") ? "com.rongzer.rdp.ec" : "com.rongzer.rdp")
                        .replace("aid", artifactId)
                        .replace("vsn", version);
                System.out.println(rst);

                //do excec
                //Process process = Runtime.getRuntime().exec(rst);
                //BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
                //String line = "";
                //while ((line = input.readLine()) != null) {
                //    processList.add(line);
                //}
                //input.close();
                //
                //for (String ln : processList) {
                //    System.out.println(ln);
                //}

            } else {
                break;
            }
        }
    }


    /**
     * 功能：批量引入maven的pom
     */
    @Deprecated
    public static void printFiles(LinkedList<File> linkedList) {
        String sb = "<dependency><groupId>com.</groupId><artifactId>com.activiti.BBB</artifactId><version>1.0.0</version><type>jar</type></dependency>";
        for (Iterator<File> iterator = linkedList.iterator(); iterator.hasNext(); ) {
            File file = iterator.next();
            if (file.isFile()) {
                String fileName = file.getName();
                System.out.println(sb.replace("BBB", fileName.substring(0, fileName.lastIndexOf("."))));
                linkedList.remove(file);
                printFiles(linkedList);
            } else {
                break;
            }
        }
    }
}
